package com.xiao.xin.ollama.client.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.xiao.xin.ollama.client.api.OllamaApi;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/***
 * 配置 Retrofit
 */
@Configuration
public class OllamaConfig {


    /***
     * 配置Ollama 请求地址
     * @param objectMapper
     * @return
     * @throws IOException
     */
    @Bean
    public OllamaApi ollamaApi(ObjectMapper objectMapper) throws IOException {

        // 开启日志支持
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        // 指定Retrofit 配置
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(loggingInterceptor)
                .readTimeout(50, TimeUnit.SECONDS)
                .connectTimeout(10, TimeUnit.SECONDS)
                .build();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://192.168.100.129:11434/") // 指定地址
                .client(okHttpClient)
                .addConverterFactory(JacksonConverterFactory.create(objectMapper))
                .build(); // 指定配置
        OllamaApi ollamaApi = retrofit.create(OllamaApi.class); // 创建
        return ollamaApi;
    }

}
